//
// Created by DengLibin on 2019/1/16 0016.
//
#include <iostream>
#include <string>
using  namespace std;

/**
 * 二进制求和
 */
 int max_67(int x, int y){
     return x > y ? x : y;
 }

string addBinary(string a, string b) {

    char a_i = '0';
    char b_i = '0';
    char result ='0'; //求和的结果
    char temp ='0'; //进位
    int len_a = a.length();
    int len_b = b.length();
    int max = max_67(len_a, len_b);
    char s[max+ 2]; //多两个用于 \0结尾和进位
    s[max+1] ='\0'; //结尾
    int index = max-1 ;
    int index_a = len_a -1;
    int index_b = len_b-1;
    while (index >= 0){
        if(index_a < len_a && index_a >=0){
            a_i = a[index_a];
        }else{
            a_i = '0';
        }
        if(index_b < len_b && index_b >=0){
            b_i = b[index_b];
        }else{
            b_i = '0';
        }

        if(a_i == '0' && b_i == '0'){
            result = temp;
            temp = '0';
        }else if(a_i == '1' && b_i =='1'){
            result = temp;
            temp='1';//进位
        }else{
            if(temp=='1'){
                result = '0';
                //temp ='1';
            }else{
                result = '1';
               // temp == '0';
            }
        }
        s[index+1] = result;
        index_a--;
        index_b--;
        index--;
    }
    string x;
    if(temp =='1'){
        s[0] = temp;
        x = s;
    }else{
        s[0] = '0';
        x = &s[1];
    }
    return x;

}

int main67(){
    string s1 = "110010";
    string s2 = "100";
    cout << addBinary(s1, s2);
    return 0;
}


